package com.xindacloud.sms.adapter.handler;


import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestControllerAdvice
@ConditionalOnMissingClass("com.xindacloud.sms.portal.config")
@SuppressWarnings("ALL")
public class SmsInnerExceptionHandler extends AbstractSmsExceptionHandler {

    @ExceptionHandler
   // @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Object transferInnerExceptionResponseBody(Exception e, HttpServletResponse response) {
        log.error("SmsInnerExceptionHandler", e);
        Object err = transferException(e);
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Content-Type", "application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(HttpStatus.OK.value());
        try {
            response.getWriter().write(new Gson().toJson(err));
        } catch (IOException exc) {
            log.error("输出错误信息IO中断", exc);
        }
        return null;
    }

}


